// pages/couponBagCenter/hotGoods.js
const controller=require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNumber:1,
    timeState:"",
    lastPage:false,
    type:'',
    selectIndex:0,
    allNum:0,
    noUseNum:0,
    historyNum:0
  },

  //获取tab数
  getNum(){
    let value1={}
    value1.pageNumber=1
    value1.timeState=''
    value1.type=this.data.type
    value1.shopId=wx.getStorageSync('shopId')
    let value2={}
    value2.pageNumber=1
    value2.timeState=1
    value2.type=this.data.type
    value2.shopId=wx.getStorageSync('shopId')
    let value3={}
    value3.pageNumber=1
    value3.timeState=2
    value3.type=this.data.type
    value3.shopId=wx.getStorageSync('shopId')
    controller.getBagList(value1).then(res=>{
      this.setData({
        allNum:res.data.totalRow
      })
    })
    controller.getBagList(value2).then(res=>{
      this.setData({
        noUseNum:res.data.totalRow
      })
    })
    controller.getBagList(value3).then(res=>{
      this.setData({
        historyNum:res.data.totalRow
      })
    })

  },
  //用户的券礼包分页列表 2.0
  getList(){
    let value={}
    value.type=this.data.type
    value.pageNumber=this.data.pageNumber
    value.timeState=this.data.timeState
    value.shopId=wx.getStorageSync('shopId')
    controller.getBagList(value).then(res=>{
      this.setData({
        list:res.data.list,
        lastPage:res.data.lastPage
      })
    })
  },


  getBagList(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      selectIndex:index
    })
    if(index===1){
      this.setData({
        timeState:1
      })
    }else if(index===2){
      this.setData({
        timeState:2
      })
    }else{
      this.setData({
        timeState:""
      })
    }
    this.getList()
  },

   //跳转券包详情
   toDetail(e){
    let state=e.currentTarget.dataset.state
    if(state==2){
     wx.showToast({
       icon:"none",
       title: '已结束',
     })
      return
    }

    let id=e.currentTarget.dataset.id
    let orderNo=e.currentTarget.dataset.orderno
    let shopId=e.currentTarget.dataset.shopid
    let type=e.currentTarget.dataset.type
   if(type==4){
    wx.navigateTo({
      url: '/pages/couponBagCenter/limitCouponDetail?id='+id+"&shopId="+shopId,
    }) 
   }else{
    wx.navigateTo({
      url: '/pages/myCenter/tradingAreaDetail?id='+id+'&orderNo='+orderNo,
    })
   }
  },

  getMore(){
    if(this.data.lastPage==false){
      let value={}
      let pageNumber=this.data.pageNumber+1
      this.setData({
        pageNumber:pageNumber
      })
      value.type=this.data.type
      value.timeState=this.data.timeState
      value.pageNumber=this.data.pageNumber
      controller.getBagList(value).then(res=>{
        let list=this.data.list
        let list2=res.data.list
        let list3=list.concat(list2)
        this.setData({
          list:list3,
          lastPage:res.data.lastPage
        })
      })
    }
  },

  //返回店铺
  toShop(e){
    let shopId=e.currentTarget.dataset.shopid
    wx.reLaunch({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNum()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})